<template>
  <div>
    <!-- 质量改善跟踪 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="120" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :dataSource="dataList"
      :datakey="'records'"
      :fetchapi="fetchApi"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :isSelectColumn="false"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary"  :click="exportFileBtn.bind()">导出</multiuse-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="selectDelearFlag" :title="dialogTitle" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <selectDelearPage @close="Delearclose" @save="Delearsave"></selectDelearPage>
    </BaseDialog>
     <Drawer :visible.sync="visibleEdit" :title="'质量信息处理'" destroyOnClose width="70%" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <editForm @close="editclose" @save="editsave" :rowData="rowData"></editForm>
    </Drawer>
    <Drawer :visible.sync="visibleEditBJ" :title="'质量信息处理'" destroyOnClose width="70%" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <editFormBJ @close="editcloseBJ" @save="editsaveBJ" :rowData="rowData"></editFormBJ>
    </Drawer>
    <BaseDialog :visible.sync="visibleAdd" :title="'选择经销商'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <DealerList @close="closeAdd" @save="saveAdd"></DealerList>
    </BaseDialog>
    <BaseDialog :visible.sync="visible" :title="'车型新增'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <vehicle @close="close" @save="save"></vehicle>
    </BaseDialog>
  </div>
</template>
<script>
  import {getListByPage,getListForExport,exporturl} from '@/api/claim/factory/QualityInformationManagement_OEM/QualityInformationExamine';
  import editForm from './editForm.vue';
  import editFormBJ from './editFormBJ.vue';
  import selectDelearPage from './selectDelearPage.vue';
  import DealerList from '@/pages/claimDemage/tools/DealerList.vue';
  import { exportExcelFile } from '@/utils/index';
  import { getPartType } from '@/api/dictionary/index';
  import { dictionary } from '@/utils/dictMixin';
  import moment from "moment";
  import { getAppId } from '@/assets/js/auth';
  import vehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
export default {
   mixins: [dictionary],
  components: {
editForm,
    vehicle,
    editFormBJ,
    selectDelearPage,
    DealerList
  },
  data() {
    return {
      dataList: [],
      visible: false,
      visibleDrawer: false,
      selectDelearFlag: false,
      collapse: true,
      visibleEdit: false,
      visibleEditBJ: false,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchParams: {
        status: 1,xhrAbort: true
      }, //ipa 接口
      selectedRows: [], //选中经销商
      dialogTitle: '选择车辆',
      visibleAdd: false,
      fetchApi:getListByPage,
      rowData:{}
    };
  },
  created() {
    this.fetchParams.bBeginDate = moment().startOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.fetchParams.eBeginDate = moment().endOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.chuqi();
  },
  methods: {
    //搜索数据
    topFilterInitFun() {
      return [
        {
          type: 'INPUT',
          label: '选择经销商',
          fieldName: 'ownerCode',
          placeholder: '请输入',
          initialValue: '',
          unitRender: p => {
            return (
              <div>
              <multiuse-button icon="el-icon-search" onClick={() => this.chooseDealerNo()} />
            </div>
          );
          }
        },
        // { type: 'INPUT', label: '经销商名称', fieldName: 'dealerName', placeholder: '请输入', initialValue: '' },
        { type: 'SELECT', label: '质量报告状态', fieldName: 'reportStatus', placeholder: '请选择', initialValue: '', itemList: this.createDictList('5561',[55611001,55611003,55611004,55611006,55611007,55611008]) },
        { type: 'INPUT', label: '质量报告单号', fieldName: 'reportNo', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '维修工单号', fieldName: 'roNo', placeholder: '请选择', initialValue: '' },
        { type: 'INPUT', label: '索赔单号', fieldName: 'cmsClaimNo', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: 'VIN', fieldName: 'vin', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '主损件名称', fieldName: 'partName', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '主损件代码', fieldName: 'partCode', placeholder: '请输入', initialValue: '' },
         { type: 'INPUT', label: '案例编号', fieldName: 'caseNo', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '案例主题', fieldName: 'caseTitle', placeholder: '请输入', initialValue: '' },
        {
          type: 'RANGE_DATE',
          label: '案例创建日期',
          fieldName: 'bBeginDate|eBeginDate',
          style: { minWidth: '200px' },
          valueFormat: 'yyyy-MM-dd',
          initialValue: [
            moment()
              .startOf('week')
              .format('YYYY-MM-DD HH:mm:ss'),
            moment()
              .endOf('week')
              .format('YYYY-MM-DD HH:mm:ss')
          ],
          rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelCode',
          placeholder: '请输入',
          initialValue: '',
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()} />
              </div>
            );
          }
        },
      ];
    },
    //搜索获取数据
    changeHandle(val) {
      if(!val.bBeginDate) {
        this.$notify.warning({ title: '提示信息', message: '案例创建开始日期不可为空！' });
        return
      }
      if(!val.eBeginDate) {
        this.$notify.warning({ title: '提示信息', message: '案例创建结束日期不可为空！' });
        return
      }
      if(getAppId() == 'arcfox') {
        if (moment(moment(val.eBeginDate).format('YYYY-MM-DD')).diff(moment(val.bBeginDate), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '案例创建日期跨度不可超过十二个月！' });
          return
        }
      } else {
        if (moment(moment(val.eBeginDate).format('YYYY-MM-DD')).diff(moment(val.bBeginDate), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '案例创建日期跨度不可超过十二个月！' });
          return
        }
      }
      this.fetchParams=Object.assign({},val,{ status: 1,xhrAbort: false});
      console.log(val);
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" disabled={(props.row.reportStatus == '55611003') ? true : false } onClick={() => this.edit(props.row)}>
                  处理
                </multiuse-button>
              </div>
            );
          }
        },
        { title: '质量报告状态', dataIndex: 'statusStr', sorter: true },
        { title: '经销商代码', dataIndex: 'ownerCode', sorter: true },
        { title: '经销商名称', dataIndex: 'dealerName', sorter: true },
        { title: '案例编号', dataIndex: 'caseNo', sorter: true },
        { title: '案例主题', dataIndex: 'caseTitle', sorter: true },
        { title: '质量报告单号', dataIndex: 'reportNo', sorter: true },
        { title: '索赔单号', dataIndex: 'cmsClaimNo', sorter: true },
        { title: '维修工单号', dataIndex: 'roNo', sorter: true },
        { title: 'VIN', dataIndex: 'vin', sorter: true },
        { title: '返运单号', dataIndex: '', sorter: true },
        { title: '生产日期', dataIndex: 'prodDate', sorter: true ,dateFormat: 'yyyy-MM-dd' },
        { title: '车型名称', dataIndex: 'modelName', sorter: true },
        { title: '主损件代码', dataIndex: 'partCode', sorter: true },
        { title: '主损件名称', dataIndex: 'partName', sorter: true },
        { title: '行驶里程', dataIndex: 'driveMile', sorter: true },
        { title: '故障现象代码', dataIndex: 'faultCode', sorter: true },
        { title: '故障现象名称', dataIndex: 'faultName', sorter: true },
        { title: '故障出现次数', dataIndex: 'faultTimes', sorter: true },
        { title: '故障级别', dataIndex: 'levelStr', sorter: true },
        { title: '申请日期', dataIndex: 'applyAt', sorter: true, dateFormat: 'yyyy-MM-dd' },
        { title: '申请人', dataIndex: 'applyBy', sorter: true },
        { title: '审核日期', dataIndex: 'checkAt', sorter: true, dateFormat: 'yyyy-MM-dd'},
        { title: '审核人', dataIndex: 'checkBy', sorter: true }
      ];
    },
    async chuqi() {
      // console.log("进来了")
      // const res1 = await getPartType({type: 5561});
      // delete  res1.data[0]
      // delete  res1.data[3]
      // delete  res1.data[5]
      // delete  res1.data[6]
      // delete  res1.data[7]
      // if (res1.resultCode == 200) {
      //   this.topFilterInit.find(x => x.fieldName == 'reportStatus').itemList = res1.data.map(y => ({
      //     text: y.codeCnDesc,
      //     value: y.codeId
      //   }));
      // }
    },
    chooseDealerNo() {
      this.visibleAdd = true;
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    close() {
      this.visible = false;
    },
    chooseOrderNo() {
      this.visible = true;
    },
    save(obj) {
      this.topFilterInit.find(x => x.fieldName == 'modelCode').initialValue = obj[0].modelCode;
      this.visible = false;
    },
   async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000158' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // let beginSubmitDate = this.topFilterInit.find(x => x.fieldName == "bBeginDate|eBeginDate").initialValue[0];
      // let endSubmitDate = this.topFilterInit.find(x => x.fieldName == "bBeginDate|eBeginDate").initialValue[1];
      // if(!beginSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '案例创建开始日期不可为空！' });
      //   return
      // }
      // if(!endSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '案例创建结束日期不可为空！' });
      //   return
      // }
      // if(getAppId() == 'arcfox') {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '案例创建日期跨度不可超过十二个月！' });
      //     return
      //   }
      // } else {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '案例创建日期跨度不可超过十二个月！' });
      //     return
      //   }
      // }
      // let obj = Object.assign({}, this.fetchParams ,{ status: 1,xhrAbort: false});
      // obj.beginApplyDate = this.topFilterInit.find(x => x.fieldName == "bBeginDate|eBeginDate").initialValue[0];
      // obj.endApplyDate = this.topFilterInit.find(x => x.fieldName == "bBeginDate|eBeginDate").initialValue[1];
      // getListForExport(obj).then(res => {
      //   exportExcelFile(res.data, '改善质量信息');
      // });
    },
    add() {
      this.visibleAdd = true;
    },editclose() {
      this.visibleEdit = false;
    },editsave() {
      this.visibleEdit = false;
      this.fetchParams = { ...this.fetchParams };
    },
   editcloseBJ() {
    this.visibleEditBJ = false;
  },editsaveBJ() {
    this.visibleEditBJ = false;
    this.fetchParams = { ...this.fetchParams };
  },
    closeAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    saveAdd(val) {
      this.topFilterInit.find(x => x.fieldName == 'ownerCode').initialValue = val[0].dealerCode;
      this.visibleAdd = false;
    }, //经销商选择页面
    SelectDelear() {
      this.selectDelearFlag = true;
    }, //关闭
    Delearclose() {
      this.selectDelearFlag = false;
    },
    //保存
    Delearsave() {
      this.selectDelearFlag = false;
      this.fetchParams = { ...this.fetchParams };
    },
    //开启编辑
    edit(val) {
      this.rowData = val;
      if (getAppId() == 'arcfox') {
        this.visibleEdit = true;
      }else {
        this.visibleEditBJ = true;
      }
    }
  }
  // mounted() {
  // }
};
</script>

<style lang="less" scoped></style>
